package com.bub.pay.core.manager.loginWhite.impl;


import com.bub.pay.core.base.BaseManagerAbstract;
import com.bub.pay.core.dao.UserLoginWhiteDao;
import com.bub.pay.core.entity.domain.UserLoginWhite;
import com.bub.pay.core.entity.dto.merchant.QueryIpWhiteDTO;
import com.bub.pay.core.entity.vo.merchant.QueryIpWhiteVO;
import com.bub.pay.core.enums.DeleteFlagEnum;
import com.bub.pay.core.manager.loginWhite.UserLoginWhiteManager;
import com.bub.pay.framework.common.base.Cause;
import com.bub.pay.framework.common.base.Module;
import com.bub.pay.framework.common.entity.model.SessionModel;
import com.bub.pay.framework.common.exception.GlobalException;
import com.bub.pay.framework.common.util.ContextHolder;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserLoginWhiteManagerImpl extends BaseManagerAbstract<UserLoginWhite, UserLoginWhiteDao> implements UserLoginWhiteManager {

    @Resource
    private UserLoginWhiteDao userLoginWhiteDao;

    @Override
    public boolean addLoginIp(Module module ,UserLoginWhite white) {

        UserLoginWhite entity = new UserLoginWhite();
        entity.setExtendId(white.getExtendId());
        entity.setUserType(white.getUserType());
        entity.setIp(white.getIp());
        entity.setDelFlag(white.getDelFlag());
        int count = userLoginWhiteDao.selectCount(entity);
        if (count > 0){
            throw new GlobalException(module, Cause.duplicateData);
        }

        int i = userLoginWhiteDao.insertSelective(white);
        return i > 0 ? true : false;
    }

    @Override
    public PageInfo<QueryIpWhiteDTO> queryLoginIp(QueryIpWhiteVO vo) {
        //获取当前登录用户
        SessionModel sessionModel = ContextHolder.currentUserInfo();

        //创建查询条件
        Example example = new Example(UserLoginWhite.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("extendId",sessionModel.getExtendId())
                .andEqualTo("userType",sessionModel.getUserType())
                .andEqualTo("delFlag", DeleteFlagEnum.using.flag)
                .andEqualTo("state",1);

        if (StringUtils.isNotBlank(vo.getIp())){
            criteria.andLike("ip","%" + vo.getIp() + "%");
        }
        if (null != vo.getCreateId()){
            criteria.andLike("createId","%" + vo.getCreateId() + "%");
        }
        if (StringUtils.isNotBlank(vo.getCreateName())){
            criteria.andLike("createName","%"+vo.getCreateName()+"%");
        }
        if (null != vo.getStartTime()){
            criteria.andGreaterThanOrEqualTo("createDate",vo.getStartTime());
        }
        if (null != vo.getEndTime()){
            criteria.andLessThanOrEqualTo("createDate",vo.getEndTime());
        }
        example.orderBy("createDate").desc();
        //分页查询数据
        PageHelper.startPage(vo.getPageNo(),vo.getPageSize());
        List<UserLoginWhite> list = userLoginWhiteDao.selectByExample(example);
        List<QueryIpWhiteDTO> dtoList = new ArrayList<>();
        long total = 0;
        if (list instanceof Page) {
            total = ((Page) list).getTotal();
        }

        if (CollectionUtils.isNotEmpty(list)){
            dtoList = list.stream().map(m -> {
                QueryIpWhiteDTO dto = new QueryIpWhiteDTO();
                BeanUtils.copyProperties(m,dto);
                return dto;
            }).collect(Collectors.toList());
        }
        PageInfo pageInfo = new PageInfo<>(dtoList);
        pageInfo.setTotal(total);
        return pageInfo;
    }

    @Override
    public boolean deleteUserLoginIp(Long id) {
        UserLoginWhite white = new UserLoginWhite();
        white.setId(id);
        white.setDelFlag(true);
        return userLoginWhiteDao.updateByPrimaryKeySelective(white) > 0 ? true : false;
    }

}